package com.gljx.web.service.impl;

import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.JournalEntry.AdmissionNumberSummaryJournal;
import com.gljx.web.Entity.JournalEntry.StudentStateJournal;
import com.gljx.web.dao.StudentMapper;
import com.gljx.web.service.AdmissionNumberSummaryJournaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2018/1/2 0002.
 */
@Service
public class AdmissionNumberSummaryJournaServiceImpl implements AdmissionNumberSummaryJournaService {

    @Autowired
    StudentMapper studentMapper;

    @Override
    public List<AdmissionNumberSummaryJournal> selectSummartAsDay(Map map) {

        List<AdmissionNumberSummaryJournal> reList = new ArrayList<>();
        Set<String> sets = new LinkedHashSet<>();

        map.put("endTime", TimeFormat.sumOneMonth((String) map.get("beginTime")));
        //查询A1到c2的各科人数
        List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournals = studentMapper.selectSummartDayAsTypeAsDay(map);
        //查询二部
        map.put("fig", null);
        List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournal2 = studentMapper.selectSummartDayAsTypeTwoAndThreeAsDay(map);
        //三部
        map.put("fig", 1);
        List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournal3 = studentMapper.selectSummartDayAsTypeTwoAndThreeAsDay(map);

        //初始化
        fomatSet(sets, admissionNumberSummaryJournals);
        fomatSet(sets, admissionNumberSummaryJournal2);
        fomatSet(sets, admissionNumberSummaryJournal3);

        for (String tem : sets) {
            AdmissionNumberSummaryJournal admissionNumberSummaryJournal = new AdmissionNumberSummaryJournal();
            admissionNumberSummaryJournal.setFbizdate(tem);
            reList.add(admissionNumberSummaryJournal);
        }

        fomatData(reList, admissionNumberSummaryJournals, 0);
        fomatData(reList, admissionNumberSummaryJournal2, 1);
        fomatData(reList, admissionNumberSummaryJournal3, 2);


        return reList;
    }

    @Override
    public List<AdmissionNumberSummaryJournal> selectSummartAsMonth(Map map) {
        List<AdmissionNumberSummaryJournal> reList = new ArrayList<>();
        Set<String> sets = new LinkedHashSet<>();

        //获取年
        String year = map.get("beginTime").toString().substring(0, 4);
        map.put("beginTime", year + "-02");
        //查询开始日期之前的各个科目人数
        List<AdmissionNumberSummaryJournal> last = studentMapper.selectSummartDayAsTypeAsLastMonth(map);
        map.put("beginTime", year + "-01");
        map.put("endTime", year + "-12");
        //查询A1到c2的各科人数
        List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournals = studentMapper.selectSummartDayAsTypeAsMonth(map);
        //查询二部
        map.put("fig", null);
        //查询开始日期之前的各个科目人数
        List<AdmissionNumberSummaryJournal> last2 = studentMapper.selectSummartDayAsTypeTwoAndThreeAsLastMonth(map);
        List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournal2 = studentMapper.selectSummartDayAsTypeTwoAndThreeAsMonth(map);
        //三部
        map.put("fig", 1);
        //查询开始日期之前的各个科目人数
        List<AdmissionNumberSummaryJournal> last3 = studentMapper.selectSummartDayAsTypeTwoAndThreeAsLastMonth(map);
        List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournal3 = studentMapper.selectSummartDayAsTypeTwoAndThreeAsMonth(map);

        //初始化
        fomatSet(sets, admissionNumberSummaryJournals);
        fomatSet(sets, admissionNumberSummaryJournal2);
        fomatSet(sets, admissionNumberSummaryJournal3);

        for (String tem : sets) {
            AdmissionNumberSummaryJournal admissionNumberSummaryJournal = new AdmissionNumberSummaryJournal();
            admissionNumberSummaryJournal.setFbizdate(tem);
            reList.add(admissionNumberSummaryJournal);
        }

        fomatData(reList, admissionNumberSummaryJournal2, 1);
        fomatData(reList, admissionNumberSummaryJournal3, 2);
        fomatData(reList, admissionNumberSummaryJournals, 3);

        fomatData(reList, last, 4);
        fomatData(reList, last, 5);
        fomatData(reList, last, 6);

        return reList;
    }

    //组装数据set
    private void fomatSet(Set<String> sets, List<AdmissionNumberSummaryJournal> studentStateJournals) {
        for (AdmissionNumberSummaryJournal tem : studentStateJournals) {
            sets.add(tem.getFbizdate());
        }
    }

    private List<AdmissionNumberSummaryJournal> fomatData(List<AdmissionNumberSummaryJournal> reList, List<AdmissionNumberSummaryJournal> admissionNumberSummaryJournals, int state) {

        if (admissionNumberSummaryJournals.size() != 0 && reList.size() != 0) {
            //添加历史数据
            if (state == 4) {

                AdmissionNumberSummaryJournal tem2 = reList.get(0);

                AdmissionNumberSummaryJournal tem1 = admissionNumberSummaryJournals.get(0);
                tem2.setLjcarTypeA1(tem1.getLjcarTypeA1());

                tem2.setLjcarTypeA2(tem1.getLjcarTypeA2());

                tem2.setLjcarTypeA3(tem1.getLjcarTypeA3());

                tem2.setLjcarTypeB1(tem1.getLjcarTypeB1());

                tem2.setLjcarTypeB2(tem1.getLjcarTypeB2());

                tem2.setLjcarTypeC1(tem1.getLjcarTypeC1());

                tem2.setLjcarTypeC2(tem1.getLjcarTypeC2());

                tem2.setLjsum(tem1.getLjsum());

                return reList;

            }

            if (state == 5) {

                AdmissionNumberSummaryJournal tem2 = reList.get(0);

                AdmissionNumberSummaryJournal tem1 = admissionNumberSummaryJournals.get(0);
                tem2.setLjtwocarTypeC1(tem1.getLjtwocarTypeC1());

                return reList;

            }

            if (state == 6) {

                AdmissionNumberSummaryJournal tem2 = reList.get(0);

                AdmissionNumberSummaryJournal tem1 = admissionNumberSummaryJournals.get(0);
                tem2.setLjthreecarTypeC1(tem1.getLjthreecarTypeC1());

                return reList;

            }

            for (AdmissionNumberSummaryJournal tem : admissionNumberSummaryJournals) {
                for (AdmissionNumberSummaryJournal tem2 : reList) {
                    if (state == 0 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                        tem2.setCarTypeA1(tem.getCarTypeA1());
                        tem2.setCarTypeA2(tem.getCarTypeA2());
                        tem2.setCarTypeA3(tem.getCarTypeA3());
                        tem2.setCarTypeB1(tem.getCarTypeB1());
                        tem2.setCarTypeB2(tem.getCarTypeB2());
                        tem2.setCarTypeC1(tem.getCarTypeC1());
                        tem2.setCarTypeC2(tem.getCarTypeC2());
                        tem2.setSum(tem.getSum());
                    }
                    if (state == 1 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                        tem2.setTwocarTypeC1(tem.getTwocarTypeC1());
                    }
                    if (state == 2 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                        tem2.setThreecarTypeC1(tem.getThreecarTypeC1());

                    }
                    if (state == 3 && tem.getFbizdate().equals(tem2.getFbizdate())) {
                        tem2.setCarTypeA1(tem.getCarTypeA1());
                        tem2.setCarTypeA2(tem.getCarTypeA2());
                        tem2.setCarTypeA3(tem.getCarTypeA3());
                        tem2.setCarTypeB1(tem.getCarTypeB1());
                        tem2.setCarTypeB2(tem.getCarTypeB2());
                        tem2.setCarTypeC1(tem.getCarTypeC1());
                        tem2.setCarTypeC2(tem.getCarTypeC2());
                        tem2.setSum(tem.getSum());
                    }

                }
            }
        }

        return reList;
    }
}
